// Copyright 2007 Google Inc.  All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.connector.gdata;

import com.google.enterprise.connector.spi.ConfigureResponse;

import junit.framework.TestCase;

import java.util.Map;
import java.util.HashMap;
import java.util.Locale;

/**
 * This class is a test case that verifies several properties of the
 * GdConnectorType implementation, namely:
 * (1) Some valid URLs are accepted.
 * (2) An invalid URL is rejected.
 * (3) The URL stored in the configuration map is placed, without modifification
 * into the HTML form snippet.
 * 
 * @author amsmith@google.com (Adam Smith)
 */
public class GdConnectorTypeTest extends TestCase {

  public void testValidUrlsDetected() {
    Locale locale = Locale.getDefault();
    GdConnectorType type = new GdConnectorType();
    Map configMap = new HashMap();
    
    configMap.put(GdConnectorType.FEEDURL_KEY, "http://localhost");
    assertNull(type.validateConfig(configMap, locale));
    
    configMap.put(GdConnectorType.FEEDURL_KEY, "https://user@localhost:443/f");
    assertNull(type.validateConfig(configMap, locale));
  }
  
  public void testInvalidUrlsDetected() {
    Locale locale = Locale.getDefault();
    GdConnectorType type = new GdConnectorType();
    Map configMap = new HashMap();

    configMap.put(GdConnectorType.FEEDURL_KEY, "l0lc4ts!!!1111");
    assertNotNull(type.validateConfig(configMap, locale));
  }
  
  public void testConfigMapPopulatesForm() {
    Locale locale = Locale.getDefault();
    GdConnectorType type = new GdConnectorType();
    
    Map configMap = new HashMap();
    String nonce = "\u9762" + System.currentTimeMillis();
    configMap.put(GdConnectorType.FEEDURL_KEY, nonce);
    ConfigureResponse response = type.getPopulatedConfigForm(configMap, locale);
    String snippet = response.getFormSnippet();
    
    assertTrue(snippet.contains(nonce));
  }
}
